package com.weishu.installment.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weishu.installment.domain.shenhe.FinanceApplyStateEnum;
import com.weishu.installment.domain.shenhe.JrContractSelectRequestDto;
import com.weishu.installment.domain.shenhe.JrRentInstallmentResponseDto;
import com.weishu.installment.domain.shenhe.JrRentInstallmentSearchPage;
import com.weishu.installment.domain.zufang.SearchRentInstallmentDetailRequestDTO;
import com.weishu.installment.domain.zufang.SearchRentInstallmentDetailResponseDTO;
import com.weishu.installment.domain.TJrProduct;
import com.weishu.installment.domain.TJrRentInstallment;
import com.weishu.installment.mapper.TJrProductMapper;
import com.weishu.installment.service.TJrRentInstallmentService;
import com.weishu.installment.mapper.TJrRentInstallmentMapper;
import com.weishu.installment.utils.MyJinRongUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
* @author admin
* @description 针对表【t_jr_rent_installment(房租分期申请)】的数据库操作Service实现
* @createDate 2023-05-05 11:54:31
*/
@Service
public class TJrRentInstallmentServiceImpl extends ServiceImpl<TJrRentInstallmentMapper, TJrRentInstallment>
    implements TJrRentInstallmentService{

    @Resource
    private TJrProductMapper jrProductMapper;

    @Transactional
    @Override
    public SearchRentInstallmentDetailResponseDTO getRentInstallmentDetail(SearchRentInstallmentDetailRequestDTO dto) {
        SearchRentInstallmentDetailResponseDTO res = null;
        // 查询金融产品
        TJrProduct product = jrProductMapper.selectById(dto.getProductId());
        dto.setCreditLine(product.getCreditLine());
        try {
            res = MyJinRongUtils.getComputeFQ(dto);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        res.setProductId(dto.getProductId());
        res.setId(dto.getContractId());
        return res;
    }


    @Resource
    TJrRentInstallmentMapper mapper;
    @Override
    public List<JrRentInstallmentResponseDto> searchLoans(JrRentInstallmentSearchPage param) {
        List<JrRentInstallmentResponseDto> list = mapper.searchJrRentInstallmentPage(param);
        System.out.println("+===========================+"+list);
        return list;
    }


//    @Override
//    public void updateAuditPassRenting(JrContractSelectRequestDto contractSelect) throws ParseException {
//        System.out.println("updateAuditPassRenting房租分期申请审核处理Start...");
//        // 1. 预审（初审）
//        if(contractSelect.getApplyState() == FinanceApplyStateEnum.CSPASS.getIndex()){
//            // 1.初审通过
//            System.out.println("updateAuditPassRenting初审处理Start...");
//            System.out.println("----租房分期初审处理入参：{}"+JSON.toJSONString(contractSelect.getApplyState()));
//            mapper.updateAuditPassRenting(contractSelect);
//            System.out.println("updateAuditPassRenting初审处理End...");
//            TJrRentInstallment byId = mapper.selectById(contractSelect.getId());
//            TJrProduct tJrProduct = jrProductMapper.selectById(byId.getJrProductId());
//        }else{
//            // 2. 终审（复审）
//            // 2.1 解冻
//            CfContractEntity entity = cfContractMapper.selectById(contractSelect.getCfContractId());
//            System.out.println("auditNoPassRenting房租分期申请审核通过解冻处理Start...");
//            entity.setUpdateTime(new Date());
//            entity.setUpdateUserid(contractSelect.getLoginUserId());
//            // 冻结状态：0未冻结 1已冻结
//            entity.setFreezeStatus(FreezeStatusEnum.WDJ.getIndex());
//            // 是否房租分期,0:否 1:是
//            entity.setRentInstallmentFlg(true);
//            System.out.println("---- 房租分期申请审核通过解冻入参：{}",JSON.toJSONString(entity));
//            cfContractMapper.updateById(entity);
//            System.out.println("---- 房租分期申请审核通过解冻成功...");
//            System.out.println("auditNoPassRenting房租分期申请审核通过解冻处理End...");
//
//            // 2.2 终止合同    查询当前 合同ID下待支付状态的房租收款计划   收款状态 ：冻结状态 更改为终止状态
//            List<CwReceiptPlanEntity> entityList = selectReceiptPlanByMap(contractSelect.getCfContractId(), 0);
////			queryObjectBusinessId
//            //根据商户id获取风控公寓授信额度
//            FkBusinessCreditEntity fkBusiness = this.fkBusinessCreditService.queryObjectBusinessId(contractSelect.getLoginUserId());
//
//            logger.info("----租房分期房租变更终止状态条数：{}",JSON.toJSONString(entityList.size()));
//            logger.info("requestUpdate变更终止状态处理Start...");
//            //	2.3   收款状态 ：冻结状态 更改为终止状态
//            if(null != entityList && entityList.size()>0){
//                // 收款状态 ：冻结状态 更改为终止状态
//                // 收款计划   1：分期变更   2：租约保理变更    3：退租变更    4：普通账单(正常房租租务计划)" 变更为分期变更
//                CwReceiptPlanEntity receiptEntity = new CwReceiptPlanEntity();
//                for (CwReceiptPlanEntity record : entityList) {
//                    receiptEntity.setId(record.getId());
//                    receiptEntity.setUpdateUserid(contractSelect.getLoginUserId());
//                    receiptEntity.setSkStatus(SkStatusEnum.YZZ.getIndex());
//                    //requestUpdate.setBillType(CollectionPlanZdType.FQBG.getIndex());
//                    receiptEntity.setUpdateTime(new Date());
//                    logger.info("----租房分期房租收款状态 ：冻结状态 更改为终止状态入参：{}",JSON.toJSONString(receiptEntity));
//                    cwReceiptPlanMapper.updateById(receiptEntity);
//                    logger.info("----租房分期房租收款状态 ：冻结状态 更改为终止状态成功：{}",JSON.toJSONString(receiptEntity));
//
//                    if(fkBusiness.getHlUsedCreditline() == null) {
//                        fkBusiness.setHlUsedCreditline(record.getRentalPrice());
//                    }else {
//                        fkBusiness.setHlUsedCreditline(fkBusiness.getHlUsedCreditline().add(record.getRentalPrice()));
//                    }
//                    if(fkBusiness.getHlMonthlyUsedCreditline() != null) {
//                        fkBusiness.setHlMonthlyUsedCreditline(fkBusiness.getHlMonthlyUsedCreditline().add(record.getRentalPrice()));
//                    }
//                    fkBusiness.setUpdateTime(new Date());
//                    fkBusiness.setUpdateUserid(contractSelect.getLoginUserId());
//                    System.out.println("----风控公寓授信：减少维数公寓总授信额度入参：{}"+JSON.toJSONString(fkBusiness));
//                    fkBusinessCreditService.updateById(fkBusiness);
//                    System.out.println("----风控公寓授信：减少维数公寓总授信额度成功：{}"+JSON.toJSONString(fkBusiness));
//
//                }
//            }
//            System.out.println("requestUpdate变更终止状态处理End...");
//
//            // 2.4   复审核通过
//            System.out.println("updateAuditPassRenting终审处理Start...");
//            System.out.println("----租房分期终审处理入参：{}"+JSON.toJSONString(contractSelect.getApplyState()));
//            financeMapper.updateAuditPassRenting(contractSelect);
//            System.out.println("----租房分期终审通过...");
//            System.out.println("updateAuditPassRenting终审处理End...");
//        }
//        System.out.println("updateAuditPassRenting房租分期申请审核处理End...");
//    }


}




